package com.stackandqueue;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;

public class Solution08 {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0)+1);
        }

        PriorityQueue<int []> pd = new PriorityQueue<>((o1, o2) -> o2[1]-o1[1]);
        for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
            pd.add(new int[]{entry.getKey(),entry.getValue()});
        }
        int[] res = new int[k];
        for (int i = 0; i < k; i++) {
            res[i] = pd.poll()[0];
        }
        return res;
    }
}
